From: Joey Hess Date: Wed, 29 Jan 2025 19:37:19 +0000 (-0400) Subject: Merge branch 'master' into ospath X-Git-Tag: archive/raspbian/10.20250416-2+rpi1~1^2~6^2~158^2~57 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https:/www.github.com/%22bookmarks:///%22http:/www.example.com/cgi/%22https:/www.github.com/%22bookmarks:/?a=commitdiff_plain;h=c309edb8fb872858e7d09d625b7c5a8328e57e0e;p=git-annex.git Merge branch 'master' into ospath --- c309edb8fb872858e7d09d625b7c5a8328e57e0e diff --cc Utility/OsPath.hs index d008a9a373,59302cd53e..aec436fae4 --- a/Utility/OsPath.hs +++ b/Utility/OsPath.hs @@@ -35,89 -27,39 +35,89 @@@ import GHC.IO (unsafePerformIO import System.OsString.Encoding.Internal (cWcharsToChars_UCS2) import qualified System.OsString.Data.ByteString.Short.Word16 as BS16 #endif +#else +import System.FilePath.ByteString as X hiding (RawFilePath, getSearchPath) +import System.FilePath.ByteString (getSearchPath) +import Data.ByteString (ByteString) +import Data.Char +import Data.Word +#endif + +class OsPathConv t where + toOsPath :: t -> OsPath + fromOsPath :: OsPath -> t + +instance OsPathConv FilePath where + toOsPath = toOsPath . toRawFilePath + fromOsPath = fromRawFilePath . fromOsPath + +#ifdef WITH_OSPATH +instance OsPathConv RawFilePath where +#if defined(mingw32_HOST_OS) + toOsPath = bytesToOsPath + fromOsPath = bytesFromOsPath +#else + toOsPath = bytesToOsPath . S.toShort + fromOsPath = S.fromShort . bytesFromOsPath +#endif + +instance OsPathConv ShortByteString where +#if defined(mingw32_HOST_OS) + toOsPath = bytesToOsPath . S.fromShort + fromOsPath = S.toShort . bytesFromOsPath +#else + toOsPath = bytesToOsPath + fromOsPath = bytesFromOsPath +#endif -toOsPath :: RawFilePath -> OsPath #if defined(mingw32_HOST_OS) -- On Windows, OsString contains a ShortByteString that is --- utf-16 encoded. So have to convert the input to that. --- This is relatively expensive. -toOsPath = unsafePerformIO . encodeFS . fromRawFilePath +-- utf-16 encoded. But the input RawFilePath is assumed to +-- be utf-8. So this is a relatively expensive conversion. +bytesToOsPath :: RawFilePath -> OsPath +bytesToOsPath = unsafePerformIO . encodeFS . fromRawFilePath #else -toOsPath = OsString . PosixString . S.toShort +bytesToOsPath :: ShortByteString -> OsPath +bytesToOsPath = OsString . PosixString #endif -fromOsPath :: OsPath -> RawFilePath #if defined(mingw32_HOST_OS) +bytesFromOsPath :: OsPath -> RawFilePath -- On Windows, OsString contains a ShortByteString that is --- utf-16 encoded. So have to convert the input from that. --- This is relatively expensive. -fromOsPath = toRawFilePath . cWcharsToChars_UCS2 . BS16.unpack . getWindowsString . getOsString +-- utf-16 encoded, but RawFilePath is utf-8. +-- So this is relatively expensive conversion. - bytesFromOsPath = toRawFilePath . cWcharsToChars_UCS2 . BS16.unpack . getWindowsString ++bytesFromOsPath = toRawFilePath . cWcharsToChars_UCS2 . BS16.unpack . getWindowsString . getOsString #else -fromOsPath = S.fromShort . getPosixString . getOsString +bytesFromOsPath :: OsPath -> ShortByteString +bytesFromOsPath = getPosixString . getOsString #endif +{- For some reason not included in System.OsPath -} +getSearchPath :: IO [OsPath] +getSearchPath = map toOsPath <$> PB.getSearchPath + +{- Used for string constants. -} +literalOsPath :: ShortByteString -> OsPath +literalOsPath = toOsPath + #else -{- When not building with WITH_OSPATH, use FilePath. This allows - - using functions from legacy FilePath libraries interchangeably with - - newer OsPath libraries. +{- When not building with WITH_OSPATH, use RawFilePath. -} -type OsPath = FilePath +type OsPath = RawFilePath + +type OsString = ByteString + +instance OsPathConv RawFilePath where + toOsPath = id + fromOsPath = id -type OsString = String +instance OsPathConv ShortByteString where + toOsPath = S.fromShort + fromOsPath = S.toShort -toOsPath :: RawFilePath -> OsPath -toOsPath = fromRawFilePath +unsafeFromChar :: Char -> Word8 +unsafeFromChar = fromIntegral . ord -fromOsPath :: OsPath -> RawFilePath -fromOsPath = toRawFilePath +literalOsPath :: RawFilePath -> OsPath +literalOsPath = id #endif